home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLASSSRC.PAK / CMDLINE.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  2KB  |  78 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1994, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.3  $
  6. //
  7. // class TCmdLine implementation
  8. //----------------------------------------------------------------------------
  9. #include <classlib/pch.h>
  10. #include <classlib/cmdline.h>
  11. #include <services/memory.h>
  12. #include <tchar.h>
  13.  
  14. const _TCHAR whitespace[] = " \t";
  15. const _TCHAR terminator[] = "=/ \t";  // remove /- to dissallow separating there
  16.  
  17. TCmdLine::TCmdLine(const _TCHAR far* cmdLine)
  18. {
  19.   Buffer = new _TCHAR[_tcslen(cmdLine)+1];
  20.   _tcscpy(Buffer, cmdLine);
  21.   Reset();
  22. }
  23.  
  24. void TCmdLine::Reset()
  25. {
  26.   Token = TokenStart = Buffer;
  27.   TokenLen = 0;
  28.   Kind = Start;
  29. }
  30.  
  31. TCmdLine::~TCmdLine()
  32. {
  33.   delete [] Buffer;
  34. }
  35.  
  36. TCmdLine::TKind TCmdLine::NextToken(bool removeCurrent)
  37. {
  38.   // Done parsing, no more tokens
  39.   //
  40.   if (Kind == Done)
  41.     return Kind;
  42.  
  43.   // Move Token ptr to next token, by copying over current token, or by ptr
  44.   // adjustment. TokenStart stays right past previous token
  45.   //
  46.   if (removeCurrent) {
  47.     _tcscpy(TokenStart, Token+TokenLen);
  48.     Token = TokenStart;
  49.   }
  50.   else {
  51.     Token += TokenLen;
  52.     TokenStart = Token;
  53.   }
  54.  
  55.   // Adjust token ptr to begining of token & determine kind
  56.   //
  57.   Token += _tcsspn(Token, whitespace);  // skip leading whitespace
  58.   switch (*Token) {
  59.     case 0:
  60.       Kind = Done;
  61.       break;
  62.     case '=':
  63.       Kind = Value;
  64.       Token++;
  65.       break;
  66.     case '-':
  67.     case '/':
  68.       Kind = Option;
  69.       Token++;
  70.       break;
  71.     default:
  72.       Kind = Name;
  73.   }
  74.   Token += _tcsspn(Token, whitespace);  // skip any more whitespace
  75.   TokenLen = _tcscspn(Token, terminator);
  76.   return Kind;
  77. }
  78.